package client;

import java.awt.BorderLayout;
import java.awt.event.MouseListener;
import java.rmi.Naming;

import javax.swing.JFrame;
import javax.vecmath.Quat4d;
import javax.vecmath.Vector3d;

import server.controller.Universe3DControllerRemoteInterface;

import client.controller.Viewpoint3DController;
import client.controller.Virtual3DController;
import client.controller.Object3DController;
import client.controller.ReceiverUpdates;
import client.controller.Universe3DController;
import client.interactor.Interactor;
import client.interactor.InteractorBehavior;
import client.interactor.MouseInteractorBehavior;
import client.interactor.PickerBehavior;
import client.interactor.TwoDPickerBehavior;
import client.interactor.WiimoteInteractorBehavior;
import client.presentation.TwoDCreator;
import client.presentation.TwoDNavigator;

import com.intel.bluetooth.BlueCoveConfigProperties;


public class Client {

	public static String IPAdress = "ThoMacBook.local";
	public static String broadcastAdress = "224.98.13.67";
	public static int port = 1099;
	public static String serverName = "ServerUniverse";
	public static String lookupName = "//"+IPAdress+":"+port+"/"+serverName;

	public static void main(String args[]) {
				
		System.setProperty(BlueCoveConfigProperties.PROPERTY_JSR_82_PSM_MINIMUM_OFF, "true");
		
		try {
			//Creation de la fenetre
			JFrame frame = new JFrame("EVC");
			frame.getContentPane().setLayout(new BorderLayout());
			
			//Recuperation du serveur
			Universe3DControllerRemoteInterface server = (Universe3DControllerRemoteInterface) Naming.lookup(lookupName);

			//Inscription du client sur le serveur
			String clientName = server.newClientName();

			//Creation du monde 3D
			Universe3DController universe = new Universe3DController();
			ReceiverUpdates receiver = new ReceiverUpdates(broadcastAdress, port);
			receiver.setDeportedClient(universe);
			
			//Creation d'un point de vue
			Viewpoint3DController viewpoint = new Viewpoint3DController("viewpoint1"+clientName, new Vector3d(0, 0, 1), new Quat4d(), "vrml/coneVert.wrl");
			viewpoint.setUniverse(universe);
			universe.addViewpoint(viewpoint);
			universe.selectViewpoint(viewpoint);
			
			//Mise en place des moyens d'interaction
			Interactor interactor = new Interactor(universe);
			PickerBehavior picker = new TwoDPickerBehavior(universe.getPickCanvas(), interactor, universe);
			InteractorBehavior interactorBehavior = new WiimoteInteractorBehavior(interactor, picker);
			InteractorBehavior mouseEvents = new MouseInteractorBehavior(interactor, picker);
			universe.getCanvas().addMouseListener((MouseListener) mouseEvents);
			
			//Ajout de moyens d'interaction a l'ihm
			TwoDNavigator navigator = new TwoDNavigator(interactorBehavior);
			TwoDCreator creator = new TwoDCreator(universe);
			
			//Mise en place de l'ihm
			frame.getContentPane().add(navigator, BorderLayout.WEST);
			frame.getContentPane().add(universe.getCanvas(), BorderLayout.CENTER);
			frame.getContentPane().add(creator, BorderLayout.EAST);
			
			//Demarrage du recepteur d'evenements serveur
			receiver.start();
			

			
			//Envoi de nos points de vue au serveur
			for(Virtual3DController tmpV : universe.getAllViewpoints()) {
				server.addViewpoint3DController(tmpV.getName(), tmpV.getPosition(), tmpV.getOrientation(), tmpV.getFile());
			}

			//Recuperation de tous les objets du serveur
			for(String n : server.getAllObjectsController()) {
				Virtual3DController localCtrl = new Object3DController(n);
				universe.addObject(localCtrl);
			}
			
			frame.pack();
			frame.setVisible(true);
			frame.setSize(800, 600);

		} catch (Exception e) {
			e.printStackTrace();
		}
	} 
}
